package com.fmi.timedealer.reporting;

import java.text.SimpleDateFormat;
import java.util.List;

import com.fmi.timedealer.R;
import com.fmi.timedealer.reporting.model.CustomReportItem;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomReportItemsListActivity extends ListActivity{

	private List<CustomReportItem> reportItems;
	private CustomReportListAdapter adapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.report_items_list);
		
		reportItems = CustomReportActivity.getReportItems();
		
		TextView lblInfo = (TextView) findViewById(R.id.lbl_Info);

		if (reportItems == null || reportItems.isEmpty()) {
			lblInfo.setText("No activities to display");
		}
		else {
			lblInfo.setText("Report");
		}
		
		adapter = new CustomReportListAdapter(this, R.layout.report_option_item);
        setListAdapter(adapter);
    }
	
	private class CustomReportListAdapter extends ArrayAdapter<CustomReportItem>{
	
		public CustomReportListAdapter(Context context, int textViewRecourceId) {
			super(context, textViewRecourceId);
			for(CustomReportItem option : reportItems) {
				add(option);
			}
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			CustomReportItem currentOption = reportItems.get(position);

			View v = convertView;
			if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.custom_report_item, null);
            }
            
            if (currentOption != null) {
            	ImageView iv = (ImageView) v.findViewById(R.id.listItemIcon);
            	iv.setImageResource(android.R.drawable.ic_dialog_info);
            	TextView tvActivityNameAndCatgory = (TextView)v.findViewById(R.id.activtyNameAndCategory);
                TextView tvSpentTime = (TextView)v.findViewById(R.id.SpentTime);
                TextView tvDate = (TextView)v.findViewById(R.id.StartDate);
                if (tvActivityNameAndCatgory != null) {
                  	tvActivityNameAndCatgory.setText(currentOption.getActivityName() + 
                  			"/" + currentOption.getActivityCategory());
                }
                if (tvSpentTime != null){
                   	tvSpentTime.setText(currentOption.getTimeSpentInHours());
                }
                if (tvDate != null) {
                 	SimpleDateFormat dateformat = new SimpleDateFormat("MMM dd yyyy");
                  	tvDate.setText(dateformat.format(currentOption.getDate()));
                }
            }
			return v;
		}
	}
}
